package com.yjc.drygoods.ware.service.impl;

import com.rabbitmq.client.Channel;
import com.yjc.common.to.OrderTo;
import com.yjc.common.to.StockLockTo;
import com.yjc.drygoods.ware.service.WareSpuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author yjc
 * @create 2022-09-28 9:02
 */
@Slf4j
@RabbitListener(queues = {"stock.release.stock.queue"})
@Service("WareCloseListener")
public class WareCloseListener {

    @Autowired
    WareSpuService wareSkuService;

    @RabbitHandler
    public void handleStockLock(StockLockTo stockLockTo, Message message, Channel channel) throws IOException {
        log.info("进入解锁库存方法");
        try {
            wareSkuService.unLocked(stockLockTo);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitHandler
    public void handleStockLock(OrderTo orderTo, Message message, Channel channel) throws IOException {
        try {
            wareSkuService.unLocked(orderTo);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
